﻿using System;
using System.Collections.Generic;
using System.Data;

namespace UDS.Components
{
    /// <summary>
    /// Maintains a cache for the map between a user's username and his/her real name.
    /// </summary>
    internal class StaffNameMap
    {
        #region NameCacheEntry class
        private class NameCacheEntry
        {
            public string RealName
            {
                get;
                private set;
            }

            /// <summary>
            /// Indicates whether the cache entry is expired or not.
            /// </summary>
            public bool IsExpired
            {
                get { return TimeExpired < DateTime.UtcNow; }
            }

            public DateTime TimeExpired
            {
                get;
                private set;
            }

            public NameCacheEntry(string realName)
            {
                if (string.IsNullOrEmpty(realName))
                {
                    throw new ArgumentNullException("realName");
                }

                RealName = realName;
                TimeExpired = DateTime.UtcNow.AddMinutes(CacheDuration);
            }
        }
        #endregion NameCacheEntry class

#if DEBUG
        private const int CacheDuration = 1; // 1 minute
#else
        private const int CacheDuration = 60; // 1 hour
#endif
        private Dictionary<string, NameCacheEntry> _map;

        internal StaffNameMap()
        {
            _map = new Dictionary<string, NameCacheEntry>(StringComparer.InvariantCultureIgnoreCase);
        }

        public string GetRealName(string username, bool allowCache)
        {
            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentNullException("username");
            }

            if (allowCache)
            {
                NameCacheEntry entry;
                lock (_map)
                {
                    if (_map.TryGetValue(username, out entry) && !entry.IsExpired)
                    {
                        return username;
                    }
                }
            }

            // The user's info has not been cached. We'll read it directly from DB
            string realName = GetRealNameByUsername(username);
            if (!string.IsNullOrEmpty(realName))
            {
                // Cache the realname
                lock (_map)
                {
                    _map[username] = new NameCacheEntry(realName);
                }
            }
            return realName;
        }

        private static string GetRealNameByUsername(string Username)
        {
            var parameters = new[]
            {
                Database.MakeInParam("@Username", SqlDbType.NVarChar, 30, Username),
            };

            // run the stored procedure
            using (var data = new Database())
            {
                using (var dr = data.RunDataReader("dbo.sproc_GetRealNameByUsername_V2", parameters))
                {
                    if (dr.Read())
                    {
                        return (string)dr[0];
                    }
                }
            }
            return string.Empty;
        }
    }
}
